package com.fei.jvmtest;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;

public class FindClassFromJar {

	//(Ljava/lang/String; Ljava/lang/String; I[Ljava/lang/String; Ljava/lang/Long; JLjava/util/HashSet;)V	getJarName
	public static void getJarName(String jarFile,String a , int b , String[] args,  Long d ,long s , HashSet<String> set) throws Exception {

		try {
			// 通过将给定路径名字符串转换为抽象路径名来创建一个新File实例
			File f = new File(jarFile);
			URL url1 = f.toURI().toURL();
			URLClassLoader myClassLoader = new URLClassLoader(new URL[] { url1 },
					Thread.currentThread().getContextClassLoader());

			// 通过jarFile和JarEntry得到所有的类
			JarFile jar = new JarFile(jarFile);
			// 返回zip文件条目的枚举
			Enumeration<JarEntry> enumFiles = jar.entries();
			JarEntry entry;

			StringBuilder sb = new StringBuilder();
			StringBuilder reg = new StringBuilder();
			reg.append("^com.sun*").append("|")
				.append("^java.applet*").append("|")
				.append("^javax.sound*").append("|")
				.append("^javax.swing*").append("|")
				.append("^org.omg*").append("|")
				.append("^sun.*").append("|")
				.append("^java.awt*").append("|")
				.append("^java.beans*").append("|")
				.append("^java.rmi*").append("|")
				.append("^javax.rmi*").append("|")
				.append("^java.security*").append("|")
				.append("^javax.security*").append("|")
				.append("^java.sql*").append("|")
				.append("^javax.sql*").append("|")
				.append("^javax.tools*").append("|")
				.append("^javax.xml*").append("|")
				.append("^java.text*").append("|")
				.append("^javax.accessibility*").append("|")
				.append("^javax.script*").append("|")
				.append("^javax.print*").append("|")
				.append("^javax.lang.model*").append("|")
				.append("^javax.activation*").append("|")
				.append("^javax.annotation*").append("|")
				.append("^javax.imageio*").append("|")
				.append("^javax.jws*").append("|")
				.append("^javax.management*").append("|")
				.append("^javax.naming*").append("|")
				.append("^org.*")
				;
			Pattern pattern = Pattern.compile(reg.toString());
			// 测试此枚举是否包含更多的元素
			while (enumFiles.hasMoreElements()) {
				entry = (JarEntry) enumFiles.nextElement();
				if (entry.getName().indexOf("META-INF") < 0) {
					String classFullName = entry.getName();
					if (!classFullName.endsWith(".class")) {
						classFullName = classFullName.substring(0, classFullName.length() - 1);
					} else {
						// 去掉后缀.class
						String className = classFullName.substring(0, classFullName.length() - 6).replace("/", ".");
						Class<?> myclass = myClassLoader.loadClass(className);
						// 得到类中包含的属性
						/*
						 * Method[] methods = myclass.getMethods(); for (Method
						 * method : methods) { String methodName =
						 * method.getName(); System.out.println("方法名称:" +
						 * methodName); Class<?>[] parameterTypes =
						 * method.getParameterTypes(); for (Class<?> clas :
						 * parameterTypes) { // String parameterName =
						 * clas.getName(); String parameterName =
						 * clas.getSimpleName(); System.out.println("参数类型:" +
						 * parameterName); }
						 * System.out.println("==========================");
						 * 
						 * 
						 * }
						 */
						
						Matcher matcher = pattern.matcher(className);
						if(matcher.find()){
							System.out.println(className);
							continue;
						}
						

						// 判断当前类是否为接口
						if (myclass.isInterface()) {
							sb.append(className).append(" ");
							System.out.println(className);
						}
					}
				}
				
				File file = new File("interface.txt");
				if(!file.exists()){
					file.createNewFile();
				}
				
				FileUtils.write(file, sb.toString());
			}
		} catch (

		IOException e) {
			e.printStackTrace();
		}
		
	}

	/**
	 * 这些默认方法不打印
	 */
	private static String DEFAULT_METHOD = "waitequalsnotifynotifyAlltoStringhashCodegetClass";

	public static void main(String[] args) throws Exception {
		// jar包所在路径
		/*
		 * getJarName("F:\\user.jar"); getJarName("F:\\role1.jar");
		 * getJarName("F:\\role2.jar");
		 */
		// getJarName("F:\\UserInfo.jar");
//		getJarName("D:\\java\\jdk1.7.0_79\\jre\\lib\\rt.jar");

	}

}